import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../config/global_configs.dart';
import '../../login/page/user_manager.dart';
import '../../net/api/http_api.dart';
import '../../res/styles.dart';
import '../../service/sos_warning_service.dart';
import '../../ui/waring/set_warning_info_page.dart';
import '../../util/request_url_utils.dart';
import '../../widgets/click_item.dart';
import 'package:base_flutter/base_flutter.dart';

class SosSettingPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => _SosSettingState();

}

class _SosSettingState extends State<SosSettingPage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('报警设置'),),
      body: Column(
        children: [
          ClickItem(
            title: '报警接收设置',
            titleStyle: TextStyles.textDark16,
            onTap: () async {
              // Go().push(PrinterHome());
              var data = await Go().push(SetWarningInfoPage());
              if(data!=null){
                _setSosService();
              }
            },
          ),

          IconTitleWidget(
            '接收报警信息',
            showArrow: false,
            contentWidget: Switch(
              value: GlobalConfigs.receiveSos,
              onChanged: (val) {
                setState(() {
                  GlobalConfigs.saveReceiveSos(val);
                });
                if(val){
                  _setSosService();
                }else{
                  SosWarningService.instance.stopService();
                }
              },
            ),
          ),

          IconTitleWidget(
            '通知栏显示',
            showArrow: false,
            contentWidget: Switch(
              value: GlobalConfigs.showSosInStatusBar,
              onChanged: (val) {
                setState(() {
                  GlobalConfigs.saveShowSosInStatusBar(val);
                });
              },
            ),
          ),

          IconTitleWidget(
            '批量执行',
            showArrow: false,
            contentWidget: Switch(
              value: GlobalConfigs.batchDealWarning,
              onChanged: (val) {
                setState(() {
                  GlobalConfigs.saveBatchWarning(val);
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _setSosService() async {
    if (await SosWarningService.instance.startService()) {
      Future.delayed(Duration(milliseconds: 1000), () {
        print("startService");
        SosWarningService.instance.sendUrlMsg(
            HttpApi.baseUrl, RequestUrlUtils.branchId,
            accountName: UserManager.userName ?? "admin111");
        SosWarningService.instance.sendEnableReceive(GlobalConfigs.receiveSos);
      });
      await SpUtil.putString("url", HttpApi.baseUrl);
      await SpUtil.putString("branchId", RequestUrlUtils.branchId);
    }
  }

}